package com.example.smarthome.ui.home;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.smarthome.R;
import com.example.smarthome.ui.masterbedroom.Datapoints;
import com.example.smarthome.ui.masterbedroom.Datastreams;
import com.example.smarthome.ui.masterbedroom.JsonRootBean;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class FanCollect extends AppCompatActivity {

    private static final String DeviceID2="1064814621";
    private static final String ApiKey ="1SM9vECD95xFxkRzxivzktUNQko=";
    TextView data1,data2,data3;
    String value1,value2,value3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fan_collect);
        EditText editText=(EditText) findViewById(R.id.Edix_text_wendu);
        Button button1= (Button) findViewById(R.id.add);
        Button button2= (Button) findViewById(R.id.reduce);
        Button button3= (Button) findViewById(R.id.home_confirm);


    }


    public void lightControl( String index) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                String cmd=index;
                String body=String.format(cmd);
                String url=String.format("http://api.heclouds.com/cmds?device_id="+DeviceID2);
                OkHttpClient client=new OkHttpClient();
                RequestBody requestBody =RequestBody.create(MediaType.parse("application/json;charset=utf-8"),body);
                Request request=new Request.Builder().url(url).header("api-key",ApiKey).post(requestBody).build();
                Call call=client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        String insertStr="请求失败";
                        Log.i("请求失败","22");
//                        Toast.makeText(MasterbedroomFragment.this,insertStr, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                    }
                });
            }
        }).start();
    }
    private void parseJSONWithGSON(String jsonData){
        JsonRootBean app =new Gson().fromJson(jsonData,JsonRootBean.class);
        List<Datastreams> streams =app.getData().getDatastreams();
        List<Datapoints> points =streams.get(0).getDatapoints();
        int count=app.getData().getCount();
        for (int i=0;i< points.size();i++){
            String time=points.get(i).getAt();
            String value =points.get(i).getValue();
        }
    }
}